import { useResourceStore } from './../../stores/resource';
import { useAwait } from '.';
import { useLoading, useEventBus, useWait } from '@/hooks';
import { ElMessage } from 'element-plus';
import { IOverlayDataSet } from '@/types/overlay';
import { isMsgOK, isOK } from '@/utils/resUtils';
import { apiList } from '@/api/apiList';
import { ref } from 'vue';

function useOverlay() {

    const overlayDatasetList = ref<IOverlayDataSet[]>([])

    // const {
    //     getNewPagination,
    //     resetPagination,
    //     setPagination,
    // } = usePagination()

    // const pagination = getNewPagination()

    const showNewPlan = ref(false)

    const planModel = ref({
        bbox: [0, 0, 0, 0],
        cron: '',
        satellite_type: [],
    })


    function handleNewPlan() {

        apiList.getOverlayConfig({
            code: 'overlay_map'
        }).then(res => {

            const { details } = res[0]
            // @ts-ignore
            planModel.value = details

        })

        showNewPlan.value = true
    }

    async function handlePlanConfirm() {

        const {
            bbox,
            cron,
            satellite_type
        } = planModel.value

        try {
            const res = await apiList.resetOverlay({
                bbox,
                cron,
                satellite_type
            })

            if (isMsgOK(res)) {
                ElMessage.success('保存成功!')

                showNewPlan.value = false
            }
        } catch (error) {
            console.warn('请求异常');

        }

    }

    const {
        loading,
        stop,
        tableLoading
    } = useLoading()

    async function getOverlayDataSetList() {

        loading()
        try {
            const res = await apiList.queryOverlayMaps({})

            if (isOK(res)) {
                // .....
                overlayDatasetList.value = res.result || []

                stop()
            }
        } catch (error) {
            console.warn('请求异常', error);

        }
    }

    // TODO:
    function handleSearch() {

    }

    // const emitter = useEventBus()

    const resStore = useResourceStore()

    function handlePreviewOverlaySnap(ds: IOverlayDataSet) {

        resStore.previewSnap = ds
        resStore.pageMode = 'map'

    }
    

    return {
        getOverlayDataSetList,
        overlayDatasetList,
        showNewPlan,
        planModel,
        handleNewPlan,
        handlePlanConfirm,
        tableLoading,
        handlePreviewOverlaySnap,
    }
}

export default useOverlay
